<?php

namespace zhtfrontend\modules\cardmanage\controllers;

use common\components\helpers\Fun;
use common\models\datas\CardAttr;
use common\models\datas\CardReSend;
use common\models\datas\CardsAttr;
use common\models\datas\CardSend;
use common\models\datas\CardsIssuerinfor;
use common\models\datas\CardsResend;
use common\models\datas\CardsSend;
use common\models\datas\IsuserInfo;
use yii\helpers\ArrayHelper;
use zhtfrontend\modules\cardmanage\components\rest\ActiveController;
use yii\web\Response;

class CardreisuserController extends ActiveController
{
    public $modelClass = 'common\models\datas\CardsIssuerinfor';

    public $serializer = [
        'class' => 'zhtfrontend\modules\cardmanage\components\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view']);
        return $actions;
    }
    public function actionIndex(){
        $modelClass = $this->modelClass;
        $data = \Yii::$app->request->get();
        $user_id =ArrayHelper::keyExists('user_id', $data) ? $data['user_id'] : '';
        $review=ArrayHelper::keyExists('review', $data) ? $data['review'] : 2;
        $site=ArrayHelper::keyExists('site', $data) ? $data['site'] : 2;
        if($site=='qdlcaks'){
            $site=2;
        }
        $isuserinfor = $modelClass::find()->where(['site_id' => $site, 'user_id' => $user_id])->all();//不再有父级和子级，只能你添加的二级发卡人
        $isuserinfor_data = [];
        $send_number=0;
        $card_attr_count=0;
        $card_attr_active=0;
        $card_send_active=0;
        if ($user_id!==''){
            foreach ($isuserinfor as $k => $v) {
                //如果是审批人的话，添加的都是一级发卡人
                foreach ($isuserinfor as $key=>$val){
                    if ($review==1){
                        $card_send=CardsSend::find()->where(['issuser_id'=>$val->id])->all();
                    }else{
                        $card_send=CardsResend::find()->where(['reissuser_id'=>$val->id])->all();
                    }
                    foreach ($card_send as $keys=>$vals){
                        $send_number=$send_number+(int)$vals->number;
                        $card_attr=CardsAttr::find()->where(['send_id'=>$vals->id])->andWhere(['between', 'card_id', $vals->cardno_begin, $vals->cardno_end])->all();
                        $card_attr_count=$card_attr_count+count($card_attr);
                        $card_attrs=CardsAttr::find()->where(['send_id'=>$vals->id,'status'=>1])->andWhere(['between', 'card_id', $vals->cardno_begin, $vals->cardno_end])->all();
                        $card_attr_active=$card_attr_active+count($card_attrs);
                    }
                    if ($card_attr_count==0){
                        $card_send_active=0;
                    }else{
                        $card_send_active=round($card_attr_active/$card_attr_count*100,1);
                    }
                    $isuserinfor_data[$key]=[
                        'id'=>$val->id,
                        'name'=>$val->name,
                        'mobile'=>$val->mobile,
                        'send_count'=>$send_number,
                        'active'=>$card_send_active,
                    ];
                    $send_number=0;
                    $card_attr_count=0;
                    $card_attr_active=0;
                    $card_send_active=0;
                }
            }
            return ['items'=>$isuserinfor_data];
        }else{
            return ['statusCode' =>0, 'message' => 'error', 'result' =>'信息不存在'];
        }
    }

    public function actionCreate(){
        $modelClass = $this->modelClass;
        $data = \Yii::$app->request->get();
        $user_id =ArrayHelper::keyExists('user_id', $data) ? $data['user_id'] : '';
        $review=ArrayHelper::keyExists('review', $data) ? $data['review'] : '';
        $reisuserName=ArrayHelper::keyExists('reisuserName', $data) ? $data['reisuserName'] : '';
        $reisuserTele=ArrayHelper::keyExists('reisuserTele', $data) ? $data['reisuserTele'] : '';
        $reisuser_review=ArrayHelper::keyExists('reisuser_review', $data) ? $data['reisuser_review'] : '';
        $password=ArrayHelper::keyExists('password', $data) ? $data['password'] : '';
        $site=ArrayHelper::keyExists('site', $data) ? $data['site'] : 2;
        if($site=='qdlcaks'){
            $site=2;
        }
        if($review==1){//如果是审批人
            $model = new $modelClass;
            $model->user_id=$user_id;
            $model->name=$reisuserName;
            $model->contact=$reisuserName;
            $model->mobile=$reisuserTele;
            $model->username=$reisuserName;
            $model->password=\Yii::$app->getSecurity()->generatePasswordHash($password);
            $model->parent_id = (int)$user_id;
            $model->review=(int)$reisuser_review;
            $model->level = 0;//leve是0的话代表的是一级发卡人
            $model->created_at = strtotime(date('Y-m-d'));
            $model->updated_at = strtotime(date('Y-m-d'));
        }else{//如果不是审批人，添加的是二级发卡人
            $issuerinfor=CardsIssuerinfor::findOne($user_id);
            $model = new $modelClass;
            $model->user_id=$user_id;
            $model->name=$reisuserName;
            $model->contact=$reisuserName;
            $model->mobile=$reisuserTele;
            $model->username=$reisuserName;
            $model->password=\Yii::$app->getSecurity()->generatePasswordHash($password);
            $model->parent_id = (int)$issuerinfor->parent_id; //如果是一级发卡人添加二级发卡人的话
            $model->review=(int)$reisuser_review;
            $model->level = 1;//leve是0的话代表的是一级发卡人
            $model->created_at = strtotime(date('Y-m-d'));
            $model->updated_at = strtotime(date('Y-m-d'));
        }
        if ($model->save(false)){
            return ['items'=>$model];
        }else{
            return ['statusCode' => 0, 'message' => 'error', 'result' => '添加发卡人失败'];
        }
    }

}